#include "Log.h"


Log::Log(const char* logName)
{
	Create(logName);
}

Log::~Log(void)
{
	Close();
}

void Log::Create(const char* logName)
{
	initialized = file.Open(logName, File::FILE_WRITE);
	if(!initialized)
	{
		Con::Printf("Cannot create log file: %s", logName);
	}	
}

void Log::Write(std::string msg)
{
	if(initialized)
	{
		file.Write((u8*)msg.c_str(), msg.length());		
	}
}
	
void Log::Write(const char* msg)
{
	if(initialized)
	{
		u32 bytes = 0;

		while(msg[bytes] != '\0')
			bytes++;

		file.Write((u8*)msg, bytes);
	}
}

void Log::Printf(const char* format, ...)
{
	if(initialized)
	{
		char msg[4096] = {};

		va_list args;
		va_start(args, format);

		if( _vsnprintf_s(msg, sizeof(msg), format, args) != -1)
		{
			u8 bytes = 0;

			while(msg[bytes] != '\0')
				bytes++;

			file.Write((u8*)msg, bytes);
		}

		va_end(args);
	}
}

void Log::Close()
{
	if(initialized)
	{
		file.Close();
	}
}


	